﻿#include "FrameDepacker.h"
#include <QByteArray>
#include "igbtprotocol.h"

FrameDepacker::FrameDepacker()
	: Depacker()
{
}


FrameDepacker::~FrameDepacker()
{
}

unsigned int FrameDepacker::depack(QByteArray* frame)
{
    if (_buf_used < 9)
		return 0;

    unsigned short header = _buf[0];
    header <<= 8;
    header += _buf[1];

    if (!(header == IGBTProtocol::FrameHeader_Cmd ||
            header == IGBTProtocol::FrameHeader_Data))
	{
		remove(1);
		return depack(frame);
	}

    unsigned int len = _buf[8];
    len <<= 8;
    len += _buf[7];
    len <<= 8;
    len += _buf[6];
    len <<= 8;
    len += _buf[5];

    if(_buf_used < len + 10)
        return 0;

    frame->append((char*)_buf, len + 10);
    remove(len + 10);
    return len + 10;
}
